package com.api.test;

import com.api.client.model.ApiLoginHandler;
import com.api.client.model.ApiProxyFactory;

public class ApiScribeImpl extends AbstractApi {

	private ApiProxyFactory apiFactory;

	private ApiLoginHandler loginHandler;

	public ApiScribeImpl(ApiLoginHandler loginHandler, ApiProxyFactory apiFactory) {

		if (loginHandler == null || apiFactory == null) {

			throw new IllegalArgumentException("LoginHandler and ApiFactory cannot be null");
		}

		this.apiFactory = apiFactory;
		this.loginHandler = loginHandler;
	}

	@Override
	public void login(String username, String password) {

		loginHandler.login(username, password);
	}

	public void setAccess(String accessKey, String accessSecret) {

		loginHandler.setAccessKeys(accessKey, accessSecret);
	}

	@Override
	public String getAccessKey() {

		if (loginHandler.isLoggedIn()) {

			return loginHandler.getAccessKey();
		} else {
			throw new IllegalStateException("User is not logged in");
		}
	}

	@Override
	public String getAccessSecret() {
		if (!loginHandler.isLoggedIn()) {
			throw new IllegalStateException("User is not logged in");
		}
		return loginHandler.getAccessSecret();
	}

	@Override
	public boolean isLoggedIn() {
		return loginHandler.isLoggedIn();
	}

	@Override
	protected <T> T getProxyService(Class<T> serviceClass) {
		return apiFactory.getProxy(serviceClass);
	}
}
